// pages/user/messages/messages.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsArr:[],
    pageNum:'1',
    touch_start:0,
    touch_end:0,
  },

  touchStart:function(e){
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-start')
  },

  touchEnd:function(e){
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-end')
  },

  deleteClick:function(e){
    console.log('删除');
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除该消息',
      success: function (res) {
        if (res.confirm) {
          console.log('点击确定');
          app.post('notice/del', { id: e.currentTarget.id}).then((res) => {
            console.log('success');
            console.log(res);
            if (res.code == 0) {
              
              that.setData({
                newsArr:[],
                pageNum: '1',
              })
              that.getData();
            } else {
              wx.showToast({
                title: res.msg,
                icon:'none',
              })
            }
          }).catch((errMsg) => {
            console.log('fail');
            console.log(errMsg + '失败');
          })
        }
      }
    })
  },

  newsClick:function(e){
    let that = this;
    //触摸时间距离页面打开的毫秒数
    var touchTime = that.data.touch_end - that.data.touch_start;
    console.log(touchTime);
    //如果按下时间大于350为长按
    if (touchTime < 350) {
      if(e.currentTarget.dataset.state == 1){
        wx.navigateTo({
          url: '../../news/newsDetail/newsDetail?url=' + e.currentTarget.dataset.url,
        })
      }
    }
  },

  getData: function () {
    var that = this;
    app.post('notice/list', {pageNum: that.data.pageNum, openId: wx.getStorageSync('userInfo').userId,pageNum:that.data.pageNum}).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        that.setData({
          newsArr: that.data.newsArr.concat(res.resultData),
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})